package com.jfinal.demo.config;

import com.jfinal.config.*;
import com.jfinal.demo.controller.HelloController;
import com.jfinal.demo.interceptor.GlobalInterceptor;
import com.jfinal.demo.model.Blog;
import com.jfinal.demo.model.User;
import com.jfinal.demo.plugins.QuartzFactory;
import com.jfinal.demo.plugins.QuartzPlugin;
import com.jfinal.demo.plugins.TestJob;
import com.jfinal.demo.plugins.TestJob2;
import com.jfinal.demo.routes.AdminRoutes;
import com.jfinal.demo.routes.FormRoutes;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.tx.TxByActionMethods;
import com.jfinal.plugin.activerecord.tx.TxByRegex;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.ViewType;


/**
 * Title:
 * Description:
 *
 * @version 2.0
 * @author: Yung Fu(FUYUNGCN AT GMAIL DOT com)
 * @since: 14-9-21 上午10:56
 */
public class DemoConfig extends JFinalConfig {

    Logger logger  = Logger.getLogger(this.getClass());

    @Override
    public void configConstant(Constants me) {
        me.setDevMode(true);
        me.setViewType(ViewType.JSP);
    }

    @Override
    public void configRoute(Routes me) {
        me.add("/hello", HelloController.class);
        me.add(new AdminRoutes());// 后端路由
        me.add(new FormRoutes());// 前端路由
    }

    @Override
    public void configPlugin(Plugins me) {

        loadPropertyFile("a_little_config.txt");
        // c3p0 plugin配置
        C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password"));

        // druid plugin配置
//        DruidPlugin druidPlugin = new DruidPlugin();

        me.add(c3p0Plugin);
        ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
        me.add(arp);
        arp.addMapping("user", User.class);// 表的主键默认为id，如果不是id的话，需要指定主键
        arp.addMapping("blog", Blog.class);

        me.add(new QuartzPlugin());
    }

    @Override
    public void configInterceptor(Interceptors me) {
        //此处将拦截所有Action
        me.add(new GlobalInterceptor());

        me.add(new TxByRegex(".*update*.*"));//为所有Action中包含update字符的方法加入声明式事务
//        me.add(new TxByActionKeys("/admin/updateUser"));//为所有admin/update开头的方法加入声明式事务
        me.add(new TxByActionMethods("save", "update"));//对方法中指定的method开启申明式事务
    }

    @Override
    public void configHandler(Handlers me) {

    }

    @Override
    public void afterJFinalStart() {
        logger.info("===============after jfinal start===========");
        String startTime = "2014-10-31 13:49:00";
        QuartzFactory.startJobOnce(startTime, 5, "test", "testgroup", TestJob.class);
        QuartzFactory.startJobForeach(10, 5, "test", "testgroup-112", TestJob2.class);
    }

    @Override
    public void beforeJFinalStop() {
        logger.info("============before jfinal stop============");
    }
}
